package com.xiaolin.base.test;

/**
 * @ClassName Solution
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/10/26
 * @Version 1.0
 */

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        StringBuilder sb1=new StringBuilder();
        while(l1.next != null){
            sb1.append(l1.val);
            l1=l1.next;
        }
        sb1.append(l1.val);
        StringBuilder sb2=new StringBuilder();
        while(l2.next !=null){
            sb2.append(l2.val);
            l2=l2.next;
        }
        sb2.append(l2.val);
        long n1=Long.parseLong(sb1.reverse().toString());
        long n2=Long.parseLong(sb2.reverse().toString());
        String sum=new StringBuilder(n1+n2+"").reverse().toString();
        ListNode l3=new ListNode();
        for(int i=0;i<sum.length();i++){
            l3.val=Integer.parseInt(sum.charAt(i)+"");
            l3.next=new ListNode();
        }
        return l3;
    }

    public static void main(String[] args) {
        ListNode l1=new ListNode();
        l1.val=2;
        l1.next=new ListNode(1);
        ListNode l2=new ListNode();
        l2.val=1;
        l2.next=new ListNode(1);
        ListNode listNode = new Solution().addTwoNumbers(l1, l2);
        System.out.println(listNode.val);
        System.out.println(listNode.next.val);

    }
}
